package com.china08.yxyapi.api.comm;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.entity.db.res.YxyResSemester;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.ResModel;
import com.china08.yxyapi.service.updateresource.ResourceUpdateService;

@CrossOrigin
@RestController
public class UpdateResourceController {

    @Autowired
    private ResourceUpdateService resourceUpdateService;

    @RequestMapping(value = "/api_yw/comm/updateresource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResModel process(@RequestBody ReqModel reqModel) {
        List<YxyResPhase> phases = reqModel.phases;
        List<YxyResSubject> subjects = reqModel.subjects;
        List<YxyResVersion> versions = reqModel.versions;
        List<YxyResBook> books = reqModel.books;
        List<YxyResSemester> semesters = reqModel.semesters;
        List<YxyResBookContents> bookContents = reqModel.bookContents;
        ResModel model = new ResModel();
        try {
            // 更新基础数据
            resourceUpdateService.updateResourceBase(phases, subjects, versions, books, semesters, bookContents);
            model.success("成功了");
        } catch (Exception e) {
            model.error(e.getMessage() + "===========" + e.toString());
            e.printStackTrace();
        }
        return model;
    }

    protected static class ReqModel {
        public List<YxyResPhase> phases;
        public List<YxyResSubject> subjects;
        public List<YxyResVersion> versions;
        public List<YxyResBook> books;
        public List<YxyResSemester> semesters;
        public List<YxyResBookContents> bookContents;
    }

}
